package cvi.com.reader.base;

import android.content.Context;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WP on 2020/7/20.
 */

public abstract class MyBaseAdapter<T> extends BaseAdapter {

    protected Context context;

    protected List<T> data;

    protected BaseFragment baseFragment;
    protected BaseActivity baseActivity;

    public MyBaseAdapter(Context context, List<T> data) {

        this.context = context;

        this.data = data == null ? new ArrayList<T>() : new ArrayList<T>(data);

    }

    public <Z extends BaseFragment> MyBaseAdapter(Context context, List<T> data, Z fragment) {

        this.context = context;

        this.data = data == null ? new ArrayList<T>() : new ArrayList<T>(data);

        this.baseFragment = fragment;

    }

    public <Y extends BaseActivity> MyBaseAdapter(Context context, List<T> data, Y activity) {

        this.context = context;

        this.data = data == null ? new ArrayList<T>() : new ArrayList<T>(data);

        this.baseActivity = activity;

    }

    @Override
    public int getCount() {

        return data.size();

    }

    @Override
    public Object getItem(int position) {

        return data.get(position);

    }

    @Override
    public long getItemId(int position) {

        return position;

    }


    public abstract int getItemResource();


    public abstract View getItemView(int position, View convertView, ViewHolder holder);

    @SuppressWarnings("unchecked")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if (null == convertView) {

            convertView = View.inflate(context, getItemResource(), null);

            holder = new ViewHolder(convertView);

            convertView.setTag(holder);

        } else {

            holder = (ViewHolder) convertView.getTag();

        }

        return getItemView(position, convertView, holder);

    }

    public class ViewHolder {

        private SparseArray<View> views = new SparseArray<View>();

        private View convertView;

        public ViewHolder(View convertView) {

            this.convertView = convertView;

        }

        @SuppressWarnings("unchecked")
        public <T extends View> T getView(int resId) {

            View v = views.get(resId);

            if (null == v) {

                v = convertView.findViewById(resId);

                views.put(resId, v);

            }

            return (T) v;

        }

    }

    public void addAll(List<T> elem) {

        data.addAll(elem);

        notifyDataSetChanged();

    }

    public void remove(T elem) {

        data.remove(elem);

        notifyDataSetChanged();

    }

    public void remove(int index) {

        data.remove(index);

        notifyDataSetChanged();

    }

    public void replaceAll(List<T> elem) {

        data.clear();

        data.addAll(elem);

        notifyDataSetChanged();

    }

}

